package com.exam.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.exam.dao.SubjectMapper;
import com.exam.domain.Question;
import com.exam.domain.Subject;
import com.exam.handler.StpHandler;
import com.exam.service.ExamService;
import com.exam.service.QuestionService;
import com.exam.utils.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "题目管理")
@RestController
@RequestMapping(value = "/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;


    /**
     * 上传科目
     * @param subjectName 科目信息
     * @return 科目信息
     */
    @GetMapping("/postSubject")
    @ApiOperation(value = "教师上传科目", notes = "教师上传科目")
    public ResultBody postSubject(@RequestParam String subjectName) {
        return ResultBody.success(questionService.postSubject(subjectName));
    }

    /**
     * 上传知识点
     * @param knowledgePointName 科目信息
     * @return 科目信息
     */
    @GetMapping("/postKnowledgePoint")
    @ApiOperation(value = "教师上传知识点", notes = "教师上传知识点")
    public ResultBody postKnowledgePoint(@RequestParam String knowledgePointName, @RequestParam int subjectId) {
        return ResultBody.success(questionService.postKnowledgePoint(knowledgePointName, subjectId));
    }

    /**
     * 上传题目
     * @param question 题目信息
     * @return 题目信息
     */
    @PostMapping("/postQuestion")
    @ApiOperation(value = "教师上传题目", notes = "教师上传题目")
    @SaCheckPermission("teacher")
    public ResultBody postQuestion(@RequestBody Question question) {
        return ResultBody.success(questionService.postQuestion(false, question));
    }

    /**
     * 根据id获取题目
     * @param id 题目id
     * @return 包括知识点和教师的题目信息
     */
    @GetMapping("/getById/{id}")
    @ApiOperation(value = "根据id获取题目", notes = "根据id获取题目")
    public ResultBody getById(@PathVariable int id) {
        return ResultBody.success(questionService.getQuestionById(id));
    }

    /**
     * 获取全部题目（分页）
     * @param index 页码
     * @param pageSize 每页条目数
     * @return 题目列表
     */
    @GetMapping("/getAll")
    @ApiOperation(value = "获取全部题目", notes = "获取全部题目")
    public ResultBody getQuestionList(@RequestParam int index, @RequestParam int pageSize) {
        return ResultBody.success(questionService.getQuestionList(index, pageSize));
    }

    /**
     * 获取教师题目（分页）
     * @param index 页码
     * @param pageSize 每页条目数
     * @return 题目列表
     */
    @GetMapping("/getByTeacher")
    @ApiOperation(value = "获取该教师的题目", notes = "获取该教师的题目")
    @SaCheckPermission("teacher")
    public ResultBody getQuestionListByTeacher(@RequestParam int index, @RequestParam int pageSize) {
        return ResultBody.success(questionService.getQuestionListByTeacher(index, pageSize, StpHandler.getId()));
    }

    /**
     * 组卷部分获取题目
     * @param subjectId 科目id
     * @return 某知识点的题目列表
     */
    @GetMapping("/getByExamPaper")
    @ApiOperation(value = "组卷部分获取题目", notes = "组卷部分获取题目")
    @SaCheckPermission("teacher")
    public ResultBody getByExamPaper(@RequestParam int subjectId, @RequestParam String type) {
        return ResultBody.success(questionService.getByExamPaper(subjectId, type, StpHandler.getId()));
    }

    /**
     * 获取全部科目列表
     * @return 科目列表
     */
    @GetMapping("/getSubjectList")
    @ApiOperation(value = "获取科目列表", notes = "根据科目列表")
    @SaCheckPermission("teacher")
    public ResultBody getSubjectList() {
        return ResultBody.success(questionService.getSubjectList());
    }

    /**
     * 根据科目id获取知识点
     * @param subjectId 科目id
     * @return 知识点列表
     */
    @GetMapping("/getKnowledgePointListBySubjectId")
    @ApiOperation(value = "根据科目id获取知识点", notes = "根据科目id获取知识点")
    @SaCheckPermission("teacher")
    public ResultBody getKnowledgePointListBySubject(@RequestParam int subjectId) {
        return ResultBody.success(questionService.getKnowledgePointListBySubject(subjectId));
    }

    /**
     * 获取按科目分类的知识点列表
     * @return 科目列表
     */
    @GetMapping("/getKnowledgePointList")
    @ApiOperation(value = "获取按科目分类的知识点列表", notes = "获取按科目分类的知识点列表")
    @SaCheckPermission("teacher")
    public ResultBody getKnowledgePointList() {
        return ResultBody.success(questionService.getKnowledgePointList());
    }

}
